package org.grow.present.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.grow.present.domain.Bankcard;
import org.grow.present.domain.Idcard;
import org.grow.present.dto.Msg;
import org.grow.present.interceptor.AuthInterceptor;
import org.grow.present.mapper.IdcardMapper;
import org.grow.present.service.IdcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/idCard")
public class IdcardController {


    @Autowired
    private IdcardService idcardService;
    @Autowired
    private IdcardMapper idcardMapper;

    @PostMapping("/saveCurrentIdCard")
    public Idcard saveCurrentIdCard(@RequestBody Idcard idcard){
        Integer UID = AuthInterceptor.getUID();
        /*   select  *  from idcard where comId = {uid}   */
        QueryWrapper<Idcard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comId",UID);
        Idcard db = idcardService.getOne(queryWrapper);
        if(db == null){
//            新增
            idcard.setId(0);
            idcard.setComId(UID);
            idcardService.save(idcard);
            return idcardService.getOne(queryWrapper);
        }else{
//            修改
            idcard.setId(db.getId());
            idcard.setComId(db.getComId());
            idcardService.updateById(idcard);
            return idcard;
        }

    }
    @GetMapping("/getCurrent")
    public Msg<Idcard> getCurrent() {
        Integer uid = AuthInterceptor.getUID();
        QueryWrapper<Idcard> w = new QueryWrapper<>();
        w.eq("comId", uid);
        Idcard db = idcardService.getOne(w);
        if(db==null){
            return Msg.fail().mess("目标不存在");
        }
        return Msg.success().data(db);
    }
}
